package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pojo.HistoryRecording;
import pojo.Recording;
import dao.book.imp.BookDaoImp;
import dao.recording.imp.RecordingDaoImp;

/**
 * Servlet implementation class ListServlet
 */
public class ListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession sen = request.getSession();
		Map<String, String> map = new HashMap<String, String>();
		RecordingDaoImp rdi = new RecordingDaoImp();
		BookDaoImp bdi = new BookDaoImp();
		List<HistoryRecording> listBook = new ArrayList<>();
		String bookName;
		HistoryRecording historyRecording;
		List<Recording> list = rdi.findAll();
		for(Recording recording:list){
			bookName = bdi.findOne(recording.getBookId()).getName();
			historyRecording = new HistoryRecording(bookName,recording.getOrderTime());
			listBook.add(historyRecording);
		}
		sen.setAttribute("history", listBook);
		response.sendRedirect("../list.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
